package com.huawei.control.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.huawei.bean.OrdersDetail;
import com.huawei.model.OrderModel;
import com.huawei.utils.EasyFactory;
import com.huawei.utils.MyUtils;

/**
 * Servlet implementation class orderQueryControl
 */
@WebServlet("/orderQueryControl")
public class orderQueryControl extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public orderQueryControl() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 使用form表单的条件进行查询
				// 将表单转换为参数实体类
				OrdersDetail orderB = MyUtils.convertRequest2BeanWithFileUpload(request, OrdersDetail.class);
				// 调用Medol进行业务逻辑
				List<OrdersDetail> lst = EasyFactory.getNewInstance(OrderModel.class).queryOrdersDetail();
				request.getSession().setAttribute("ordersDetail", lst.get(0));
				System.out.println("lst::" + lst.toString());
				// 把查询结果集转换为json字符串格式，借助于第三方工具 Alibaba 的 fastjson工具 （进行了日期格式转换处理）

				String strJson = JSON.toJSONStringWithDateFormat(lst, "yyyy-MM-dd HH:mm:ss",
						SerializerFeature.WriteDateUseDateFormat);

				System.out.println("Json::" + strJson);

				// 获取response中的流操作对象PrintWriter	
				request.setCharacterEncoding("utf-8"); 
				response.setContentType("text/html;charset=utf-8");
				PrintWriter pw = response.getWriter();
			
				// 写出内容到浏览器客户端
				pw.write(strJson);
				pw.flush();
	}

}
